package com.yuncommunity.runcircle;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.yuncommunity.runcircle.base.MyActivity;
import com.yuncommunity.runcircle.conf.UserInfo;
import com.yuncommunity.runcircle.db.DBHelper;
import com.yuncommunity.runcircle.item.HistoryItem;
import com.yuncommunity.runcircle.item.LocationItem;
import com.yuncommunity.runcircle.util.MyUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by oldfeel on 11/22/15.
 */
public class HistoryDetail extends MyActivity {
    @Bind(R.id.map)
    MapView mapView;
    @Bind(R.id.distance)
    TextView distance;
    @Bind(R.id.time)
    TextView time;
    @Bind(R.id.speed)
    TextView speed;
    @Bind(R.id.calories)
    TextView calories;
    private HistoryItem item;
    private AMap aMap;
    private Polyline polyline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_detail);
        ButterKnife.bind(this);
        mapView.onCreate(savedInstanceState);
        item = (HistoryItem) getIntent().getSerializableExtra("item");
        showTitle(item.date);
        distance.setText(item.distance + "");
        time.setText(item.getDuration());
        speed.setText(item.getSpeed());
        calories.setText(MyUtil.getCalories(UserInfo.getInstance().getWeight(), item.distance * 1000) + "kcal");

        initMap();
    }

    private void initMap() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        List<LocationItem> list = DBHelper.getInstance(this).getLocationList(item);
        if (list == null || list.size() < 2) {
            showToast("找不到本次记录");
            return;
        }
        PolylineOptions options = new PolylineOptions();
        for (int i = 0; i < list.size(); i++) {
            options.add(new LatLng(list.get(i).lat, list.get(i).lon));
        }
        polyline = aMap.addPolyline(options);
        LatLng startLatLng = new LatLng(list.get(0).lat, list.get(0).lon);
        LatLng endLatLng = new LatLng(list.get(list.size() - 1).lat, list.get(list.size() - 1).lon);
        LatLng center = new LatLng((startLatLng.latitude + endLatLng.latitude) / 2, (endLatLng.longitude + endLatLng.longitude) / 2);
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(center, 18));

        aMap.addMarker(new MarkerOptions()
                .position(startLatLng)
                .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_start))));
        aMap.addMarker(new MarkerOptions()
                .position(endLatLng)
                .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_end))));
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        mapView.onDestroy();
        super.onDestroy();
    }
}
